package com.itqhy.controller;

import com.itqhy.pojo.*;
import com.itqhy.service.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class OrderController {
    @Resource(name = "orderService")
    private OrderService orderService;

    /**
     * 提交订单(将订单信息保存到订单表中，订单的id和商品的id插入到明细表中)
     */
    @RequestMapping("/commitOrder")
    public String commitOrder(Order order, HttpSession session, HttpServletRequest request){

        int paystate=0;//默认支付状态为0，表示未支付
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");
        String date = sdf.format(new Date());
        order.setOrdertime(date);
        order.setId(UUID.randomUUID().toString());
        User user= (User) session.getAttribute("user");
        order.setUser_id(user.getId());
        order.setPaystate(paystate);

        CartItem cartItem=new CartItem();
        orderService.saveOrder(order);
        Map<String,CartItem> map= (Map<String, CartItem>) session.getAttribute("car");
        Set<String> keys=map.keySet();
        Iterator<String> iterator=keys.iterator();

        while (iterator.hasNext()){

            cartItem=map.get(iterator.next());
            String product_id=cartItem.getProduct().getId();
            String order_id=order.getId();

            OrderItem orderItem=new OrderItem();

            orderItem.setOrder_id(order_id);
            orderItem.setProdct_id(Integer.parseInt(product_id));
            orderItem.setBuynum(cartItem.getCount());

            orderService.saveOrderIdAndProductId(orderItem);
        }

        return ""+request.getContextPath()+"/createOrderSuccess.jsp";
    }
    /**
     * 订单查询
     */
    @RequestMapping("/findOrderByUserId")
        public String findOrderByUserId(HttpSession session){

        User user= (User) session.getAttribute("user");
        int user_id=user.getId();
        //统计该用户下订单的个数
        int count=orderService.countOrderByUserId(user_id);
        //通过用户可以查询此用户下的所有订单
        List<Order> orderList=orderService.findOrderByUserId(user_id);
        session.setAttribute("count",count);
        session.setAttribute("orderList",orderList);
         return "/orderlist.jsp";
    }
    /**
     * 通过订单号查询商品的详细信息
     */
    @RequestMapping("/findProductByOrderId")
    public String findProductByOrderId(String order_id,HttpSession session){

        Order findOrderlist= (Order) orderService.findProductByOrderId(order_id);
        session.setAttribute("findOrderlist",findOrderlist);

        return "/orderInfo.jsp";
    }
    /**
     * 如果未支付，点击到支付界面中，显示出订单号和金额
     */
    @RequestMapping("/payMomey")
    public String pauMomey(String order_id,HttpSession session){
        Order findOrderlist= (Order) orderService.findProductByOrderId(order_id);
        session.setAttribute("findOrderlist",findOrderlist);

        return "/pay.jsp";
    }

    /**
     * 删除订单表中的数据和订单明细中的内容
     */
    @RequestMapping("/deleteOrderById")
    public String deleteOrderById(String order_id){

        orderService.deleteOrderById(order_id);
        orderService.deleteOrderItemById(order_id);
        return "/delOrderSuccess.jsp";


    }


}
